package com.cqvie.service;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cqvie.controller.msg.ClassesMsg;
import com.cqvie.controller.msg.FindAttendMsg;
import com.cqvie.controller.msg.FindLeaveMsg;
import com.cqvie.controller.msg.LoginMsg;
import com.cqvie.controller.msg.StudentMsg;
import com.cqvie.controller.msg.UpdateStuMsg;
import com.cqvie.dao.AttendDao;
import com.cqvie.dao.AttendDetailsDao;
import com.cqvie.dao.ClassDao;
import com.cqvie.dao.CommonDao;
import com.cqvie.dao.CourseDao;
import com.cqvie.dao.CourseScheduleDao;
import com.cqvie.dao.LeaveMessageDao;
import com.cqvie.dao.StudentDao;
import com.cqvie.dao.TeacherDao;
import com.cqvie.dao.UserDao;
import com.cqvie.model.AttendDetails;
import com.cqvie.model.Classes;
import com.cqvie.model.Course;
import com.cqvie.model.CourseSchedule;
import com.cqvie.model.LeaveMessage;
import com.cqvie.model.Role;
import com.cqvie.model.Student;
import com.cqvie.model.Teacher;
import com.cqvie.tools.ClassScanner;

@Transactional
@Service("userService")
public class UserService {
	@Resource
	private UserDao userDao;
	@Resource
	private TeacherDao teacherDao;
	@Resource
	private CourseScheduleDao csDao;
	@Resource
	private CourseDao courseDao;
	@Resource
	private StudentDao studentDao;
	@Resource
	private ClassDao classDao;
	@Resource
	private AttendDao attendDao;
	@Resource
	private LeaveMessageDao leaveMsgDao;
	@Resource
	private AttendDetailsDao adDao;
	@Resource
	private CommonDao commonDao;
	
	// 管理员登录
	public boolean isExist(LoginMsg lm,HttpSession session) {
		if (userDao.isExist(lm.getUsername(), lm.getPassword())) {
			session.setAttribute("jurisdiction",((Role)(commonDao.findObjectByHQL("FROM Role where description='admins'"))).getName());
			session.setAttribute("username", lm.getUsername());
			return true;
		}
		return false;
	}

	// 添加学生信息
	//@Transactional(propagation=Propagation.REQUIRES_NEW)
	public boolean addStudent(StudentMsg stuMsg) {
		Student s=new Student();
		s.setStudentID(stuMsg.getStudentID());
		s.setName(stuMsg.getName());
		s.setStu_password(stuMsg.getStu_password());
		s.setClasses(classDao.GetClasses(stuMsg.getStu_class()));
		List<Student> result1=studentDao.selectByStuID(s.getStudentID());
		if(result1!=null){
			return false;
		}else{
			studentDao.addStudent(s);
			return true; 
		}
	}

	// 添加班主任信息
	public boolean addTeacherMaster(Teacher tm) {
		List<Teacher> tms=teacherDao.selectByID(tm.getTeacherID());
		if(tms!=null){
			return false;
		}else{
			teacherDao.addTeacher(tm);
			return true;
		}
	}

	// 添加教师信息
	public boolean addTeacher(Teacher t) {
		List<Teacher> listTeacher=teacherDao.selectByID(t.getTeacherID());
		if(listTeacher.size()>=1){
			return false;
		}else{
			teacherDao.addTeacher(t);
			return true;
		}
	}

	// 添加班级信息
	public boolean addClass(ClassesMsg cmsg) {
		Classes c=new Classes();
		c.setClassID(cmsg.getClassID());
		c.setClassName(cmsg.getClassName());
		c.setStuNumber(Integer.parseInt(cmsg.getStuNumber()));//强转，model类似为string
		List<Teacher> teachs=teacherDao.selectByID(Integer.valueOf(cmsg.getTeacherMaster()).intValue());
		c.setMasterTeacher(teachs.get(0));
		List<Classes> list_class=classDao.selectClassByID(c.getClassID());
		if(list_class!=null){
			return false;
		}else{
			classDao.addClass(c);
			return true;
		}
	}

	// 添加课程信息
	public boolean addCourse(Course course) {
		List<Course> list_course=courseDao.selectByID(course.getCourseID());
		if(list_course!=null){
			return false;
		}else{
			courseDao.addCourse(course);
			return true;
		}
	}

	// 添加课程安排表信息
	public boolean addCS(CourseSchedule cs) {
		csDao.addCS(cs);
		return true;
	}

	// 查询考勤记录
	public List<AttendDetails> findstuAttendDateils(FindAttendMsg fam) {
		List<AttendDetails> attends = adDao.selectBystuName(fam.getStuName());
		if (attends!=null) {
			return attends;
		} else
			return null;
	}

	// 查询请假记录
	public List<LeaveMessage> findstuLeaveMsg(FindLeaveMsg flm) {
		List<LeaveMessage> lms = leaveMsgDao.selectByIDLeaveMsg(flm
				.getLeavename());
		if (lms!=null) {
			return lms;
		} else
			return null;
	}

	// 修改学生信息
	public boolean updateStu(UpdateStuMsg stuMsg) {
		return studentDao.updatestuClass(stuMsg.getStuclassName(),
				stuMsg.getStuID());
	}
	
	
	public Map<String, String> GetService_MethodAndAnnotation(){
		return ClassScanner.getMethods();
	}
	
	
}
